package com.useeinfo.oa.modules.maintaining.biz;

import com.alibaba.fastjson.JSONArray;
import com.useeinfo.framework.extend.biz.CrudBiz;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.inspection.biz.InspectionExecuteTimeBiz;
import com.useeinfo.oa.modules.inspection.biz.PlannerInspectionBiz;
import com.useeinfo.oa.modules.inspection.entity.InspectionExecuteTime;
import com.useeinfo.oa.modules.inspection.entity.PlannerInspection;
import com.useeinfo.oa.modules.maintaining.dao.PeriodInstallDao;
import com.useeinfo.oa.modules.maintaining.entity.PeriodInstall;
import com.useeinfo.oa.modules.maintaining.entity.PlanExecuteTime;
import com.useeinfo.oa.modules.maintaining.entity.PlannerOrder;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service("periodInstallBiz")
public class PeriodInstallBiz extends CrudBiz<PeriodInstallDao, PeriodInstall> {

    @Autowired
    private PlanExecuteTimeBiz planExecuteTimeBiz;
    @Autowired
    private PlannerOrderBiz plannerOrderBiz;
    @Autowired
    private InspectionExecuteTimeBiz inspectionExecuteTimeBiz;
    @Autowired
    private PlannerInspectionBiz plannerInspectionBiz;

    //生成时间周期，维保
    @Transactional(readOnly = false)
    public ResultDto executeOrder(PlannerOrder plannerOrder) {
        //删除已生成的数据
        QueryParam queryParam = new QueryParam("-1","-1");
        queryParam.getSqlMap().put("plannerId", plannerOrder.getId() + "");
        List<PlanExecuteTime> planExecuteTimeList = planExecuteTimeBiz.findList(queryParam);
        if (!CollectionUtils.isEmpty(planExecuteTimeList)) {
            for (PlanExecuteTime planExecuteTime : planExecuteTimeList) {
                planExecuteTimeBiz.delete(planExecuteTime.getId());
            }
        }
        String next = "";
        Date nextDate = new Date();
        Date endDate=DateTimeUtils.getEndOfDay1(plannerOrder.getEndTime());
        int days = StringConverters.null2Int(plannerOrder.getRemindTime());
        PeriodInstall periodInstall = plannerOrder.getPeriodInstall();
        if (periodInstall.getPlanType().equals("1")) { //间隔
            String startTime = "";
            String endTime = "";
            String executeTime = "";
            //开始时间后的下一个周期
            nextDate = DateTimeUtils.nextDate(plannerOrder.getStartTime(), periodInstall.getType(), StringConverters.null2Int(periodInstall.getFrequency()));
            if(periodInstall.getType().equals("时")){
                if (StringUtils.isBlank(startTime)) {
                    startTime = DateTimeUtils.formatDateToStringWithTime(plannerOrder.getStartTime());
                }
                int count = 0;
                while (StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm").compareTo(endDate) < 0) {
                    //结束时间
                    endTime= DateTimeUtils.formatDateToStringWithTime(DateTimeUtils.nextTime(StringConverters.ToDateTime(startTime), periodInstall.getType(), StringConverters.null2Int(periodInstall.getFrequency())));
                    if (StringConverters.ToDatePattern(endTime, "yyyy-MM-dd HH:mm").compareTo(endDate) >= 0) {
                        endTime = DateTimeUtils.format(endDate,"yyyy-MM-dd HH:mm");
                    }
                    //提醒时间
                    executeTime = DateTimeUtils.format(DateTimeUtils.nextDate(StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm"), "日", -days), "yyyy-MM-dd HH:mm");
                    if (StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm").compareTo(new Date()) > 0) {
                        save(startTime, endTime, executeTime, plannerOrder.getId(), periodInstall, plannerOrder);
                    }
                    //下一个开始时间就是当前的结束日期
                    startTime = endTime;
                }
            }else{
                JSONArray jsonArray = JSONArray.parseArray(periodInstall.getTime());
                if (jsonArray.size() != 0) {
                    //下一个周期加上时分，
                    next = DateTimeUtils.format(nextDate, "yyyy-MM-dd ") + jsonArray.get(0);
                    if (StringUtils.isBlank(startTime)) {
                        startTime = DateTimeUtils.formatDateToStringWithOnlyDate(plannerOrder.getStartTime()) + " " + jsonArray.get(0);//开始时间加上时分
                    }
                    int count = 0;
                    while (StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm").compareTo(endDate) < 0) {
                        if (count + 1 == jsonArray.size()) {
                            //下一个结束日期
                            endTime = next;
                            next = DateTimeUtils.format(DateTimeUtils.nextDate(StringConverters.ToDatePattern(next, "yyyy-MM-dd HH:mm"), periodInstall.getType(), StringConverters.null2Int(periodInstall.getFrequency())), "yyyy-MM-dd HH:mm");
                            count = -1;
                        } else if (count + 1 < jsonArray.size()) {
                            endTime = DateTimeUtils.format(StringConverters.ToDateOnly(startTime), "yyyy-MM-dd ") + jsonArray.get(count + 1);
                        }
                        if (StringConverters.ToDatePattern(endTime, "yyyy-MM-dd HH:mm").compareTo(endDate) >= 0) {
                            endTime = DateTimeUtils.format(endDate,"yyyy-MM-dd HH:mm");
                        }
                        //提醒时间
                        executeTime = DateTimeUtils.format(DateTimeUtils.nextDate(StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm"), "日", -days), "yyyy-MM-dd HH:mm");
                        if (StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm").compareTo(new Date()) > 0) {
                            save(startTime, endTime, executeTime, plannerOrder.getId(), periodInstall, plannerOrder);
                        }
                        count++;
                        //下一个开始时间就是当前的结束日期
                        startTime = endTime;
                    }
                }
            }
        } else if (periodInstall.getPlanType().equals("2")) { //日期
            //获取月日，拼接年
            JSONArray jsonArray = JSONArray.parseArray(periodInstall.getAllDate());
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            //获取开始时间到结束时间中间的有多少个年
            int period = plannerOrder.getEndTime().getYear() - plannerOrder.getStartTime().getYear();
            String startTime = "";
            String endTime = "";
            String executeTime = "";
            for (int j = 0; j <= period; j++) {
                year = year + j;
                for (int i = 0; i < jsonArray.size(); i++) {
                    String date = year + "-" + jsonArray.get(i);//拼接今年的这个时间
                    //如果选择的日期在当前时间之后，第一次开始时间为当年选择的时间，否则为下一年当前时间
                    if (StringConverters.ToDateOnly(date).compareTo(plannerOrder.getStartTime()) >= 0) {//在当前时间之前
                        nextDate = StringConverters.ToDateOnly(date);
                    } else {
                        continue;
                    }
                    JSONArray timeArray = JSONArray.parseArray(periodInstall.getTime());

                    if (timeArray.size() != 0) {
                        if (StringUtils.isBlank(startTime)) {
                            startTime = DateTimeUtils.format(nextDate, "yyyy-MM-dd ") + timeArray.get(0);
                        }
                        int count = 0;
                        while (count < timeArray.size()&&StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm").compareTo(endDate)<0) {
                            if (count + 1 == timeArray.size()) {
                                if (i + 1 < jsonArray.size()) {
                                    endTime = cal.get(Calendar.YEAR) + "-" + jsonArray.get(i + 1) + " " + timeArray.get(0);
                                } else {
                                    endTime = (year + 1) + "-" + jsonArray.get(0) + " " + timeArray.get(0);
                                }
                            } else if (count + 1 < timeArray.size()) {
                                endTime = DateTimeUtils.format(StringConverters.ToDateOnly(startTime), "yyyy-MM-dd ") + timeArray.get(count + 1);
                            }
                            if (StringConverters.ToDatePattern(endTime, "yyyy-MM-dd").compareTo(endDate) > 0) {
                                endTime = DateTimeUtils.format(endDate,"yyyy-MM-dd HH:mm");
                            }
                            //提醒时间
                            executeTime = DateTimeUtils.format(DateTimeUtils.nextDate(StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm"), "日", -days), "yyyy-MM-dd HH:mm");
                            if (StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm").compareTo(new Date()) > 0) {
                                save(startTime, endTime, executeTime, plannerOrder.getId(), periodInstall, plannerOrder);
                            }
                            startTime = endTime;
                            count++;
                        }
                    }
                }
            }
        } else {
            //获取两个时间之间有多少个星期
            long week = DateTimeUtils.getDifferWeeksWithTwoDate(plannerOrder.getStartTime(), plannerOrder.getEndTime());
            for (int j = 0; j <= week; j++) {
                JSONArray jsonArray = JSONArray.parseArray(periodInstall.getWeek());//每周一三五
                for (int i = 0; i < jsonArray.size(); i++) {
                    nextDate = DateTimeUtils.nextDate(StringConverters.ToDatePattern(DateTimeUtils.getCurrentWeekday(StringConverters.null2Int(jsonArray.get(i)) - 1), "yyyy-MM-dd"), "周", j);
                    if (nextDate.compareTo(plannerOrder.getStartTime()) < 0) {//在开始时间的周之后，按照设定周几执行,小于当前时间的不执行
                        continue;
                    }
                    JSONArray timeArray = JSONArray.parseArray(periodInstall.getTime());
                    String startTime = "";
                    String endTime = "";
                    String executeTime = "";
                    if (StringUtils.isBlank(startTime)) {
                        startTime = DateTimeUtils.format(nextDate, "yyyy-MM-dd ") + timeArray.get(0);
                    }
                    if (timeArray.size() != 0) {

                        int count = 0;
                        while (count < timeArray.size()&&StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm").compareTo(endDate)<0) {
                            if (count + 1 == timeArray.size()) {
                                if (i + 1 < jsonArray.size()) {
                                    //获取下一个时间点
                                    nextDate = DateTimeUtils.nextDate(StringConverters.ToDatePattern(DateTimeUtils.getCurrentWeekday(StringConverters.null2Int(jsonArray.get(i + 1)) - 1), "yyyy-MM-dd"), "周", j);
                                    endTime = DateTimeUtils.format(nextDate, "yyyy-MM-dd ") + timeArray.get(0);
                                } else {
                                    //获取下一周的最初时间
                                    nextDate = DateTimeUtils.nextDate(StringConverters.ToDatePattern(DateTimeUtils.getCurrentWeekday(StringConverters.null2Int(jsonArray.get(0)) - 1), "yyyy-MM-dd"), "周", j + 1);
                                    endTime = DateTimeUtils.format(nextDate, "yyyy-MM-dd ")+ timeArray.get(0);
                                }
                            } else if (count + 1 < timeArray.size()) {
                                endTime = DateTimeUtils.format(StringConverters.ToDateOnly(startTime), "yyyy-MM-dd ") + timeArray.get(count + 1);
                            }
                            if(StringConverters.ToDatePattern(endTime,"yyyy-MM-dd HH:mm").compareTo(plannerOrder.getEndTime())>=0){
                                endTime = DateTimeUtils.format(endDate,"yyyy-MM-dd HH:mm");
                            }
                            executeTime = DateTimeUtils.format(DateTimeUtils.nextDate(StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm"), "日", -days), "yyyy-MM-dd HH:mm");
                            if (StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm").compareTo(new Date()) > 0) {
                                save(startTime, endTime, executeTime, plannerOrder.getId(), periodInstall, plannerOrder);
                            }
                            startTime = endTime;
                            count++;
                        }
                    }
                }
            }
        }
        return ResultUtil.success();
    }

    //维保生成数据
    public ResultDto save(String startTime, String endTime, String executeTime, Long id, PeriodInstall periodInstall, PlannerOrder plannerOrder) {
        String status = plannerOrder.getPlannerStatusEnum().getName();
        PlanExecuteTime planExecuteTime = new PlanExecuteTime();
        //计划在运行状态下才生成工单，operateStatus作校验，1时才生成工单，0不生成
        if ("运行中".equals(status)) {
            planExecuteTime.setOperateStatus(1);
        } else {
            planExecuteTime.setOperateStatus(0);
        }
        //判断是否有顺延
        if (plannerOrder.getDelayDate() != null && plannerOrder.getDelay()) {
            logger.info("该工单有顺延，开始时间为：" + plannerOrder.getDelayDate());
            planExecuteTime.setStartTime(DateTimeUtils.format(plannerOrder.getDelayDate(), "yyyy-MM-dd HH:mm"));
            planExecuteTime.setEndTime(DateTimeUtils.format(plannerOrder.getEndTime(), "yyyy-MM-dd HH:mm"));
            //如果提醒时间在当前时间之前，那么提醒时间为当前时间
            if(DateTimeUtils.nextDate(plannerOrder.getDelayDate(), "日", -StringConverters.null2Int(plannerOrder.getRemindTime())).compareTo(new Date())<0){
                executeTime=DateTimeUtils.format(new Date(),"yyyy-MM-dd HH:mm");
            }
            planExecuteTime.setExecuteTime(StringConverters.ToDatePattern(executeTime, "yyyy-MM-dd HH:mm"));
            plannerOrder.setDelay(false);
            plannerOrderBiz.addOrUpdate(plannerOrder);
        } else {
            planExecuteTime.setStartTime(startTime);
            planExecuteTime.setEndTime(endTime);
            //如果提醒时间在当前时间之前，那么提醒时间为当前时间
            if (StringConverters.ToDatePattern(executeTime, "yyyy-MM-dd HH:mm").compareTo(new Date())<0) {
                executeTime=DateTimeUtils.format(new Date(),"yyyy-MM-dd HH:mm");
            }
            planExecuteTime.setExecuteTime(StringConverters.ToDatePattern(executeTime, "yyyy-MM-dd HH:mm"));
        }
        logger.info("开始时间为："+startTime);
        planExecuteTime.setPlannerOrder(plannerOrder);
        planExecuteTime.setPeriodInstall(periodInstall);
        planExecuteTimeBiz.addOrUpdate(planExecuteTime);
        return ResultUtil.success();

    }

    //巡检查询需要生成的数据
    public ResultDto executeInsOrder(PlannerInspection plannerInspection) {
        //删除已生成的数据
        QueryParam queryParam = new QueryParam("-1","-1");
        queryParam.getSqlMap().put("plannerId", plannerInspection.getId() + "");
        List<InspectionExecuteTime> inspectionExecuteTimes = inspectionExecuteTimeBiz.findList(queryParam);
        if (!CollectionUtils.isEmpty(inspectionExecuteTimes)) {
            for (InspectionExecuteTime inspectionExecuteTime : inspectionExecuteTimes) {
                inspectionExecuteTimeBiz.delete(inspectionExecuteTime.getId());
            }
        }
        String next = "";
        Date nextDate = new Date();
        Date endDate=DateTimeUtils.getEndOfDay1(plannerInspection.getEndTime());
        int days = StringConverters.null2Int(plannerInspection.getRemindTime());
        PeriodInstall periodInstall = plannerInspection.getPeriodInstall();

        if (periodInstall.getPlanType().equals("1")) { //间隔
            String startTime = "";
            String endTime = "";
            String executeTime = "";
            //开始时间后的下一个周期
            nextDate = DateTimeUtils.nextDate(plannerInspection.getStartTime(), periodInstall.getType(), StringConverters.null2Int(periodInstall.getFrequency()));
            if(periodInstall.getType().equals("时")){
                if (StringUtils.isBlank(startTime)) {
                    startTime = DateTimeUtils.formatDateToStringWithTime(plannerInspection.getStartTime());
                }
                int count = 0;
                while (StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm").compareTo(endDate) < 0) {
                    //结束时间
                    endTime= DateTimeUtils.formatDateToStringWithTime(DateTimeUtils.nextTime(StringConverters.ToDateTime(startTime), periodInstall.getType(), StringConverters.null2Int(periodInstall.getFrequency())));
                    if (StringConverters.ToDatePattern(endTime, "yyyy-MM-dd HH:mm").compareTo(endDate) >= 0) {
                        endTime = DateTimeUtils.format(endDate,"yyyy-MM-dd HH:mm");
                    }
                    //提醒时间
                    executeTime = DateTimeUtils.format(DateTimeUtils.nextDate(StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm"), "日", -days), "yyyy-MM-dd HH:mm");
                    if (StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm").compareTo(new Date()) > 0) {
                        saveIns(startTime, endTime, executeTime, plannerInspection.getId(), periodInstall, plannerInspection);
                    }
                    //下一个开始时间就是当前的结束日期
                    startTime = endTime;
                }
            }else{
                JSONArray jsonArray = JSONArray.parseArray(periodInstall.getTime());
                if (jsonArray.size() != 0) {
                    //下一个周期加上时分，
                    next = DateTimeUtils.format(nextDate, "yyyy-MM-dd ") + jsonArray.get(0);
                    if (StringUtils.isBlank(startTime)) {
                        startTime = DateTimeUtils.formatDateToStringWithOnlyDate(plannerInspection.getStartTime()) + " " + jsonArray.get(0);//开始时间加上时分
                    }
                    int count = 0;
                    while (StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm").compareTo(endDate) < 0) {
                        if (count + 1 == jsonArray.size()) {
                            //下一个结束日期
                            endTime = next;
                            next = DateTimeUtils.format(DateTimeUtils.nextDate(StringConverters.ToDatePattern(next, "yyyy-MM-dd HH:mm"), periodInstall.getType(), StringConverters.null2Int(periodInstall.getFrequency())), "yyyy-MM-dd HH:mm");
                            count = -1;
                        } else if (count + 1 < jsonArray.size()) {
                            endTime = DateTimeUtils.format(StringConverters.ToDateOnly(startTime), "yyyy-MM-dd ") + jsonArray.get(count + 1);
                        }
                        if (StringConverters.ToDatePattern(endTime, "yyyy-MM-dd HH:mm").compareTo(endDate) >= 0) {
                            endTime = DateTimeUtils.format(endDate,"yyyy-MM-dd HH:mm");
                        }
                        //提醒时间
                        executeTime = DateTimeUtils.format(DateTimeUtils.nextDate(StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm"), "日", -days), "yyyy-MM-dd HH:mm");
                        if (StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm").compareTo(new Date()) > 0) {
                            saveIns(startTime, endTime, executeTime, plannerInspection.getId(), periodInstall, plannerInspection);
                        }
                        count++;
                        //下一个开始时间就是当前的结束日期
                        startTime = endTime;
                    }
                }
            }


        } else if (periodInstall.getPlanType().equals("2")) { //日期
            //获取月日，拼接年
            JSONArray jsonArray = JSONArray.parseArray(periodInstall.getAllDate());
            Calendar cal = Calendar.getInstance();
            int year = cal.get(Calendar.YEAR);
            //获取开始时间到结束时间中间的有多少个年
            int period = plannerInspection.getEndTime().getYear() - plannerInspection.getStartTime().getYear();
            String startTime = "";
            String endTime = "";
            String executeTime = "";
            for (int j = 0; j <= period; j++) {
                year = year + j;
                for (int i = 0; i < jsonArray.size(); i++) {
                    String date = year + "-" + jsonArray.get(i);//拼接今年的这个时间
                    //如果选择的日期在当前时间之后，第一次开始时间为当年选择的时间，否则为下一年当前时间
                    if (StringConverters.ToDateOnly(date).compareTo(plannerInspection.getStartTime()) >= 0) {//在当前时间之前
                        nextDate = StringConverters.ToDateOnly(date);
                    } else {
                        continue;
                    }
                    JSONArray timeArray = JSONArray.parseArray(periodInstall.getTime());

                    if (timeArray.size() != 0) {
                        if (StringUtils.isBlank(startTime)) {
                            startTime = DateTimeUtils.format(nextDate, "yyyy-MM-dd ") + timeArray.get(0);
                        }
                        int count = 0;
                        while (count < timeArray.size()&&StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm").compareTo(endDate)<0) {
                            if (count + 1 == timeArray.size()) {
                                if (i + 1 < jsonArray.size()) {
                                    endTime = cal.get(Calendar.YEAR) + "-" + jsonArray.get(i + 1) + " " + timeArray.get(0);
                                } else {
                                    endTime = (year + 1) + "-" + jsonArray.get(0) + " " + timeArray.get(0);
                                }
                            } else if (count + 1 < timeArray.size()) {
                                endTime = DateTimeUtils.format(StringConverters.ToDateOnly(startTime), "yyyy-MM-dd ") + timeArray.get(count + 1);
                            }
                            if (StringConverters.ToDatePattern(endTime, "yyyy-MM-dd").compareTo(endDate) > 0) {
                                endTime = DateTimeUtils.format(endDate,"yyyy-MM-dd HH:mm");
                            }
                            //提醒时间
                            executeTime = DateTimeUtils.format(DateTimeUtils.nextDate(StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm"), "日", -days), "yyyy-MM-dd HH:mm");
                            if (StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm").compareTo(new Date()) > 0) {
                                saveIns(startTime, endTime, executeTime, plannerInspection.getId(), periodInstall, plannerInspection);
                            }
                            startTime = endTime;
                            count++;
                        }
                    }
                }
            }
        } else {
            //获取两个时间之间有多少个星期
            long week = DateTimeUtils.getDifferWeeksWithTwoDate(plannerInspection.getStartTime(), plannerInspection.getEndTime());
            for (int j = 0; j <= week; j++) {
                JSONArray jsonArray = JSONArray.parseArray(periodInstall.getWeek());//每周一三五
                for (int i = 0; i < jsonArray.size(); i++) {
                    nextDate = DateTimeUtils.nextDate(StringConverters.ToDatePattern(DateTimeUtils.getCurrentWeekday(StringConverters.null2Int(jsonArray.get(i)) - 1), "yyyy-MM-dd"), "周", j);
                    if (nextDate.compareTo(plannerInspection.getStartTime()) < 0) {//在开始时间的周之后，按照设定周几执行,小于当前时间的不执行
                        continue;
                    }
                    JSONArray timeArray = JSONArray.parseArray(periodInstall.getTime());
                    String startTime = "";
                    String endTime = "";
                    String executeTime = "";
                    if (StringUtils.isBlank(startTime)) {
                        startTime = DateTimeUtils.format(nextDate, "yyyy-MM-dd ") + timeArray.get(0);
                    }
                    if (timeArray.size() != 0) {

                        int count = 0;
                        while (count < timeArray.size()&&StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm").compareTo(endDate)<0) {
                            if (count + 1 == timeArray.size()) {
                                if (i + 1 < jsonArray.size()) {
                                    //获取下一个时间点
                                    nextDate = DateTimeUtils.nextDate(StringConverters.ToDatePattern(DateTimeUtils.getCurrentWeekday(StringConverters.null2Int(jsonArray.get(i + 1)) - 1), "yyyy-MM-dd"), "周", j);
                                    endTime = DateTimeUtils.format(nextDate, "yyyy-MM-dd ") + timeArray.get(0);
                                } else {
                                    //获取下一周的最初时间
                                    nextDate = DateTimeUtils.nextDate(StringConverters.ToDatePattern(DateTimeUtils.getCurrentWeekday(StringConverters.null2Int(jsonArray.get(0)) - 1), "yyyy-MM-dd"), "周", j + 1);
                                    endTime = DateTimeUtils.format(nextDate, "yyyy-MM-dd ")+ timeArray.get(0);
                                }
                            } else if (count + 1 < timeArray.size()) {
                                endTime = DateTimeUtils.format(StringConverters.ToDateOnly(startTime), "yyyy-MM-dd ") + timeArray.get(count + 1);
                            }
                            if(StringConverters.ToDatePattern(endTime,"yyyy-MM-dd HH:mm").compareTo(plannerInspection.getEndTime())>=0){
                                endTime = DateTimeUtils.format(endDate,"yyyy-MM-dd HH:mm");
                            }
                            executeTime = DateTimeUtils.format(DateTimeUtils.nextDate(StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm"), "日", -days), "yyyy-MM-dd HH:mm");
                            if (StringConverters.ToDatePattern(startTime, "yyyy-MM-dd HH:mm").compareTo(new Date()) > 0) {
                                saveIns(startTime, endTime, executeTime, plannerInspection.getId(), periodInstall, plannerInspection);
                            }
                            startTime = endTime;
                            count++;
                        }
                    }
                }
            }
        }
        return ResultUtil.success();
    }

    public ResultDto saveIns(String startTime, String endTime, String executeTime, Long id, PeriodInstall periodInstall, PlannerInspection plannerInspection) {
        String status = plannerInspection.getInsPlannerStatusEnum().getName();
        InspectionExecuteTime inspectionExecuteTime = new InspectionExecuteTime();
        //计划在运行状态下才生成工单，operateStatus作校验，1时才生成工单，0不生成
        if ("运行中".equals(status)) {
            inspectionExecuteTime.setOperateStatus(1);
        } else {
            inspectionExecuteTime.setOperateStatus(0);
        }
        //判断是否有顺延
        if (plannerInspection.getDelayDate() != null && plannerInspection.isDelay()) {
            logger.info("该工单有顺延，开始时间为：" + plannerInspection.getDelayDate());
            inspectionExecuteTime.setStartTime(DateTimeUtils.format(plannerInspection.getDelayDate(), "yyyy-MM-dd HH:mm"));
            inspectionExecuteTime.setEndTime(DateTimeUtils.format(plannerInspection.getEndTime(), "yyyy-MM-dd HH:mm"));
            //如果提醒时间在当前时间之前，那么提醒时间为当前时间
            if(DateTimeUtils.nextDate(plannerInspection.getDelayDate(), "日", -StringConverters.null2Int(plannerInspection.getRemindTime())).compareTo(new Date())<0){
                executeTime=DateTimeUtils.format(new Date(),"yyyy-MM-dd HH:mm");
            }
            inspectionExecuteTime.setExecuteTime(StringConverters.ToDatePattern(executeTime, "yyyy-MM-dd HH:mm"));
            plannerInspection.setDelay(false);
            plannerInspectionBiz.addOrUpdate(plannerInspection);
        } else {
            inspectionExecuteTime.setStartTime(startTime);
            inspectionExecuteTime.setEndTime(endTime);
            //如果提醒时间在当前时间之前，那么提醒时间为当前时间
            if (StringConverters.ToDatePattern(executeTime, "yyyy-MM-dd HH:mm").compareTo(new Date())<0) {
                executeTime=DateTimeUtils.format(new Date(),"yyyy-MM-dd HH:mm");
            }
            inspectionExecuteTime.setExecuteTime(StringConverters.ToDatePattern(executeTime, "yyyy-MM-dd HH:mm"));
        }
        logger.info("开始时间为："+startTime);
        inspectionExecuteTime.setPlannerInspection(plannerInspection);
        inspectionExecuteTime.setPeriodInstall(periodInstall);
        inspectionExecuteTimeBiz.addOrUpdate(inspectionExecuteTime);
        return ResultUtil.success();

    }




}
